package com.scitc.pleasurevideo.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;
import com.scitc.pleasurevideo.util.IdGeneratorUtil;

public class DialogUserAddVideoSource {


    private Context context;

    private NewsPopupWindow popupWindow;
    private EditText ettitle;
    private EditText eturl;
    private EditText etsearchpath;
    private EditText etsubtitle;
    private EditText etplaytype;
    private EditText etisdownload;
    private EditText etisselect;
    private EditText etisuse;
    private TextView tvExitBtn;
    private TextView tvSureBtn;

    public DialogUserAddVideoSource(View view, Context context) {
        super();
        this.context = context;
        initView();
        showShare(view, 0, 0, 0);
    }

    public void showShare(View parent, int x, int y, int postion) {
        // 设置popwindow显示位置
        popupWindow.showAtLocation(parent, 0, x, y);
        // 获取popwindow焦点
        popupWindow.setFocusable(true);
        // 设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
    }

    private void initView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View popView = layoutInflater.inflate(R.layout.dialog_user_add_video_source, null);
        popupWindow = new NewsPopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.setAnimationStyle(R.style.default_dialog_show);
        popView.setOnClickListener(v -> popupWindow.dismiss());

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        initview(popView);
        onclick();

    }

    private void onclick() {
        tvExitBtn.setOnClickListener(view -> popupWindow.dismiss());
        tvSureBtn.setOnClickListener(view -> {
                addVideoSource();
                popupWindow.dismiss();
            }
        );
    }

    private void addVideoSource() {
        VideoSourceVo videoSourceVo = new VideoSourceVo();
        videoSourceVo.setSource_url_id(IdGeneratorUtil.getIdString("videosource_"));
        videoSourceVo.setSource_url_title(ettitle.getText().toString());
        videoSourceVo.setSource_url_html_str(eturl.getText().toString());
        videoSourceVo.setSource_url_search_str(etsearchpath.getText().toString());
        videoSourceVo.setSource_url_subtitle(etsubtitle.getText().toString());
        videoSourceVo.setSource_url_play_type(etplaytype.getText().toString());
        videoSourceVo.setSource_url_is_download(etisdownload.getText().toString());
        videoSourceVo.setSource_url_is_select(etisselect.getText().toString());
        videoSourceVo.setSource_url_is_use(etisuse.getText().toString());
        OkGoUtil.init().toaddVidoeSource(videoSourceVo, new HttpCompleteOrFail() {
            @Override
            public void onComplete(String json) {
                Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFile(String information) {
                Toast.makeText(context, "添加错误", Toast.LENGTH_SHORT).show();
            }
        });

    }


    private void initview(View popView) {
        ettitle = (EditText) popView.findViewById(R.id.ettitle);
        eturl = (EditText) popView.findViewById(R.id.eturl);
        etsearchpath = (EditText) popView.findViewById(R.id.etsearchpath);
        etsubtitle = (EditText) popView.findViewById(R.id.etsubtitle);
        etplaytype = (EditText) popView.findViewById(R.id.etplaytype);
        etisdownload = (EditText) popView.findViewById(R.id.etisdownload);
        etisselect = (EditText) popView.findViewById(R.id.etisselect);
        etisuse = (EditText) popView.findViewById(R.id.etisuse);
        tvExitBtn = (TextView) popView.findViewById(R.id.tvExitBtn);
        tvSureBtn = (TextView) popView.findViewById(R.id.tvSureBtn);
    }
}
